package com.chiangleoi.appointment.appointments;


import cn.hutool.core.io.IORuntimeException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.chiangleoi.appointment.utils.DateUtils;
import com.chiangleoi.appointment.utils.ProxyAuthenticator;
import lombok.extern.slf4j.Slf4j;

import java.net.Authenticator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: clover
 * @Description:
 * @Date: 2024-09-11 09:53
 */
@Slf4j
public class ProxyUtils {
    final static String pageUrl = "https://jntyzx.cn:8443/GYM-JN/multi/Subscribe/addSubscribe"; // 要访问的目标网页

    // 用户名密码认证(私密代理/独享代理)
    final static String ProxyUser = "d2758505110";
    final static String ProxyPass = "1auxzpkz";

    public static String sendPostToProxy(String json, String userName, String ip, int port) {
        try {
            // 设置请求验证信息
            Authenticator.setDefault(new ProxyAuthenticator(ProxyUser, ProxyPass));
            log.info(userName + "发送请求时间");
            long startTime = System.currentTimeMillis();
            // 发送请求
            String result = HttpRequest.post(pageUrl)
                    .header("Content-Type", "application/json")//头信息，多个头信息多次调用此方法即可
                    .header("Host", "jntyzx.cn:8443")
                    .header("Connection", "keep-alive")
                    .header("Accept-Encoding", "gzip,compress,br,deflate")
                    .header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003137) NetType/WIFI Language/zh_CN")
                    .header("Referer", "https://servicewechat.com/wxb3c812f7a3ebf851/58/page-frame.html")
                    .body(json)
                    .setHttpProxy(ip, port)
                    .timeout(20000) // 设置超时，毫秒
                    .execute().body();
            long times = System.currentTimeMillis() - startTime;
            log.info(userName + ",发送请求结束时间:{},用时:{}", DateUtils.format(new Date()), times);
            return result;
        } catch (IORuntimeException e) {
            log.info("发送请求失败IORuntimeException:{}", e.getMessage());
        } catch (Exception e) {
            log.error("发送请求失败Exception:{}", e.getMessage());
        }
        return "请求错误";
    }

    public static Map<String, String> getdps(int num) {
        Map<String, String> ipMap = new HashMap<>();
        // 获取私密代理IP API
        String apiUrl = "https://dps.kdlapi.com/api/getdps/?pt=1&format=json&sep=1&";
        // 请求参数
        String secretId = "ofugbain0eyyaw7jhspa";
        String signature = "wmik35cj0drm3fob07vblhhlwayen2ek";
        String params = "secret_id=" + secretId + "&signature=" + signature + "&num=" + num;
        String body = HttpRequest.get(apiUrl + params).execute().body();
        //   log.info("ip代理池:" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data != null) {
            List<String> ipList = data.getJSONArray("proxy_list").toJavaList(String.class);
            for (String ip : ipList) {
                String[] split = ip.split(":");
                ipMap.put("ip", split[0]);
                ipMap.put("port", split[1]);
            }
        }
        return ipMap;
    }

    public static void main(String[] args) {
        System.out.println(getdps(1).toString());
    }


}
